<template>
  <div class="ui_register">
    <van-nav-bar title="注册">
      <van-icon name="arrow-left" slot="left" color="#333" @click="back"></van-icon>
    </van-nav-bar>
    <div class="register_handle_container">
      <div class="register_mobile_number">
        <i class="iconfont icon-shouji"></i>
        <div class="register_input_box">
          <input
            class="register_input"
            type="text"
            placeholder="请输入手机号码"
            maxlength="11"
            v-model="mobileNo"
            @blur="mobileNoInputBlur"
            @input="mobileNoInputChange">
          <SmsCode :mobileNoIsValid="mobileNoIsValid" :mobileNo="mobileNo" codeType="registration"></SmsCode>
        </div>
      </div>
      <div class="register_password">
        <i class="iconfont icon-Password"></i>
        <div class="register_input_box">
          <input
            class="register_input"
            type="text"
            placeholder="短信验证码"
            maxlength="6"
            v-model="smsCode">
        </div>
      </div>
      <div class="register_btn_box">
        <button
          class="register_next_btn"
          :class="mobileNoIsValid ? '' : 'disable'"
          :disabled="!mobileNoIsValid"
          @click="goToNext">下一步</button>
      </div>
      <div class="register_protocol_box">
        注册成功即代表您已经同意<span @click="goToUserProtocol">《用户协议》</span>和<span @click="goToPrivacyPolicy">《隐私政策》 </span>
      </div>
    </div>
    <ErrorPopup :isError="isError" :errorMessage="errorMessage"></ErrorPopup>
  </div>
</template>

<script type="text/babel">
  import SmsCode from '../../component/login/SmsCode';
  import ErrorPopup from '../../component/login/ErrorPopup';

  export default {
    name: 'Register',
    components: { SmsCode, ErrorPopup },
    data() {
      return {
        mobileNo: '',
        smsCode: '',
        isError: false,
        errorMessage: '',
        mobileNoIsValid: false,
        smsCodeIsValid: false,
      };
    },
    methods: {
      back() {
        this.$router.go(-1);
      },
      mobileNoInputChange() {

        this.mobileNoIsValid = this.validateMobileNo();

      },
      mobileNoInputBlur() {
        if (!this.validateMobileNo()) {
          this.isError = true;
          this.errorMessage = '请填写正确的手机号码';
          setTimeout(() => {
            this.isError = false;
            this.errorMessage = '';
          }, 3000);
        }
      },
      validateMobileNo() {

        const mobileNoReg = /^[1][3,4,5,7,8][0-9]{9}$/;

        return mobileNoReg.test(this.mobileNo);

      },
      goToNext() {
        // 获取验证码后进入下一步
        this.$store.dispatch('register', { mobileNo: this.mobileNo, code: this.smsCode, codeType: 'registration' }).then(() => {
          this.showSmsCodeValidate(true);
          this.$router.replace('./setpassword')
        });
      },
      goToUserProtocol() {
        this.$router.push('/userprotocol');
      },
      goToPrivacyPolicy() {
        this.$router.push('/privacypolicy');
      },
      showSmsCodeValidate(show) {
        this.smsCodeIsValid = show;
      }
    }
  };
</script>

<style scoped lang="less">
  .ui_register {
    .register_handle_container {
      margin-top: 30px;
      background-color: #fff;
      padding: 30px 0;
      .register_mobile_number,
      .register_password {
        display: flex;
        align-items: center;
        height: 80px;
        margin-bottom: 40px;
        padding: 0 65px;
        .iconfont {
          flex: 0 0 auto;
          margin-right: 10px;
          color: #999;
          font-size: 40px;
        }
      }
      .register_input_box {
        display: flex;
        flex: 1 0 auto;
        height: 100%;
        border-radius: unset;
        border-bottom: 1px solid #e1e1e1;
        .register_input {
          flex: 1 0 auto;
          height: 100%;
          padding: 20px 0 20px 30px;
          outline: none;
          border: none;
          background: transparent;
          font-size: 30px;
        }
      }
      .register_btn_box {
        margin-top: 60px;
        padding: 0 30px;
        .register_next_btn {
          width: 100%;
          height: 90px;
          outline: none;
          border: none;
          border-radius: 45px;
          background-color: #f7a427;
          color: #fff;
          font-size: 36px;
          &.disable {
            background-color: #fbcb82;
          }
        }
      }
      .register_protocol_box {
        padding: 60px 0 30px 0;
        text-align: center;
        color: #999;
        font-size: 22px;
        span {
          color: #4694ff;
        }
      }
    }
  }
</style>